﻿using System;
using System.Windows.Forms;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace Core
{
    [Serializable]
    public class CheckForCommentsPolicy : PolicyBase
    {
        public override string Description
        {
            get { return "Remind users to add meaningful comments to their checkins"; }
        }

        // This is a string that is stored with the policy definition on the source
        // control server. If a user does not have the policy plug-in installed, this string
        // is displayed.  You can use this to explain to the user how they should 
        // install the policy plug-in.
        public override string InstallationInstructions
        {
            get { return "To install this policy, read InstallInstructions.txt."; }
        }

        // This string identifies the type of policy. It is displayed in the 
        // policy list when you add a new policy to a Team Project.
        public override string Type
        {
            get { return "Check for Comments Policy"; }
        }

        // This string is a description of the type of policy. It is displayed 
        // when you select the policy in the Add Check-in Policy dialog box.
        public override string TypeDescription
        {
            get { return "This policy will prompt the user to decide whether or not they should be allowed to check in."; }
        }

        // This method is called by the policy framework when you create 
        // a new check-in policy or edit an existing check-in policy.
        // You can use this to display a UI specific to this policy type 
        // allowing the user to change the parameters of the policy.
        public override bool Edit(IPolicyEditArgs args)
        {
            // Do not need any custom configuration
            return true;
        }

        // This method performs the actual policy evaluation. 
        // It is called by the policy framework at various points in time
        // when policy should be evaluated. In this example, the method 
        // is invoked when various asyc events occur that may have 
        // invalidated the current list of failures.
        public override PolicyFailure[] Evaluate()
        {
            string proposedComment = PendingCheckin.PendingChanges.Comment;
            if (String.IsNullOrEmpty(proposedComment))
            {
                return new PolicyFailure[] {
                new PolicyFailure("Please provide some comments about your check-in", this) };
            }
            else
            {
                return new PolicyFailure[0];
            }
        }

        // This method is called if the user double-clicks on 
        // a policy failure in the UI. In this case a message telling the user 
        // to supply some comments is displayed.
        public override void Activate(PolicyFailure failure)
        {
            MessageBox.Show("Please provide comments for your check-in.", "How to fix your policy failure");
        }

        // This method is called if the user presses F1 when a policy failure 
        // is active in the UI. In this example, a message box is displayed.
        public override void DisplayHelp(PolicyFailure failure)
        {
            MessageBox.Show("This policy helps you to remember to add comments to your check-ins.", "Prompt Policy Help");
        }
    }
}